package com.mis.action.sys;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jeecg.action.BaseAction;
import com.jeecg.pageModel.Json;
import com.jeecg.util.ExceptionUtil;
import com.mis.page.sys.GroupPage;
import com.mis.service.sys.GroupServiceI;
import com.opensymphony.xwork2.ModelDriven;


/**   
 * @Title: Action
 * @Description: 组织机构
 * @author zhangdaihao
 * @date 2013-01-27 18:36:15
 * @version V1.0   
 *
 */
@Action(value = "groupAction", results = { @Result(name = "group", location = "/com/mis/sys/group.jsp") })
public class GroupAction extends BaseAction implements ModelDriven<GroupPage> {

	private static final Logger logger = Logger.getLogger(GroupAction.class);

	private GroupServiceI groupService;

	private GroupPage groupPage = new GroupPage();

	public GroupPage getModel() {
		return groupPage;
	}

	public GroupServiceI getGroupService() {
		return groupService;
	}

	@Autowired
	public void setGroupService(GroupServiceI groupService) {
		this.groupService = groupService;
	}

	/**
	 * 首页获得菜单树
	 */
	public void ctrlTree() {
		writeJson(groupService.tree(groupPage, false));
	}

	/**
	 * 跳转到菜单管理页面
	 * 
	 * @return
	 */
	public String goGroup() {
		return "group";
	}

	/**
	 * 获得菜单treegrid
	 */
	public void treegrid() {
		writeJson(groupService.treegrid(groupPage));
	}

	/**
	 * 获取菜单树,递归子节点
	 */
	public void treeRecursive() {
		writeJson(groupService.tree(groupPage, true));
	}

	/**
	 * 所有人都有权限的菜单
	 */
	public void groupTreeRecursive() {
		writeJson(groupService.tree(groupPage, true));
	}

	/**
	 * 编辑菜单
	 */
	public void edit() {
		Json j = new Json();
		try {
			groupService.edit(groupPage);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
			j.setObj(groupPage.getCpid());
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	/**
	 * 添加菜单
	 */
	public void add() {
		Json j = new Json();
		try {
			groupService.add(groupPage);
			j.setSuccess(true);
			j.setMsg("添加成功!");
			j.setObj(groupPage.getCpid());
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		writeJson(j);
	}

	/**
	 * 删除一个菜单
	 */
	public void delete() {
		Json j = new Json();
		try {
			groupService.delete(groupPage);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(groupPage.getObid());
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		writeJson(j);
	}

}
